package com.bootcamp.javase2.stream;

import java.io.*;

/**
 * @program: CharacterStreamCode
 * @description: count odd and even number within a txt file.
 * @author: Keyu Li
 * @create: 2021-09-10 19:42
 **/

public class CalculateNumber {
    public static void main(String[] args) throws IOException {
        // calculate count
        // new filereader object
        BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
        char[] chars = reader.readLine().toCharArray();
        //calc odd and even number
        int countOdd = 0;
        int countEven = 0;
        for (char c : chars) {
            if (((int) c) % 2 == 0) {
                countEven++;
                continue;
            }
            countOdd++;
        }

        // store count in the file
        // new filewriter object
        BufferedWriter writer = new BufferedWriter(new FileWriter("a.txt", true));
        writer.write("   奇数：" + countOdd + "个 偶数： " + countEven + "个");
        //close
        reader.close();
        writer.close();
    }
}
